package com.salton123.ui.base

import android.os.Bundle
import com.salton123.feature.IFeature
import me.yokeyword.fragmentation.SupportActivity

/**
 * Time:6/1/21 9:20 PM
 * Author:
 * Description:
 */
abstract class LifeDelegateActivity(val componentLife: ComponentLife = ComponentLife()) : SupportActivity(),
    IComponentLife by componentLife {

    abstract fun getLayoutId(): Int
    open fun isAsyncContentView(): Boolean = true
    abstract fun initViewAndData(savedInstanceState: Bundle?) //初始化数据在View之后
    open fun initListener() {}
    open fun initLogic() {}
    private var features: MutableList<IFeature> = mutableListOf()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        componentLife.apply {
            with(this@LifeDelegateActivity)
            setContentView(getRootView())
            inflateContentView(isAsyncContentView(), getLayoutId()) {
                initViewAndData(savedInstanceState)
                initListener()
                initLogic()
                features.forEach { it.onBind() }
            }
        }
    }

    fun addFeature(feature: IFeature) {
        features.add(feature)
    }
}